package com.vtb.entity;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import com.vtb.base.VtbBaseEntity;

@SuppressWarnings("unchecked")
public class DataCache {
	public static HashMap<String, RrOauth> rrmap = new HashMap<String, RrOauth>();
	public static HashMap<String, QqOauth> qqmap = new HashMap<String, QqOauth>();
	public static HashMap<String, SsOauth> ssmap = new HashMap<String, SsOauth>();
	
	public static ConcurrentHashMap<String, String> rrlastTime = new ConcurrentHashMap<String, String>();
	public static ConcurrentHashMap<String, String> qqlastTime = new ConcurrentHashMap<String, String>();
	public static ConcurrentHashMap<String, String> sslastTime = new ConcurrentHashMap<String, String>();
	
	public static AtomicInteger pcount = new AtomicInteger();
	public static AtomicInteger mcount = new AtomicInteger();

	public static ArrayBlockingQueue<String> mailQeque = new ArrayBlockingQueue<String>(10);
	public static int getPcount(){
		return pcount.get();
	}
	public static int increment(){
		return pcount.incrementAndGet();
	}
	public static int decrement(){
		return pcount.decrementAndGet();
	}
	public static int increment(int i){
		return pcount.addAndGet(i);
	}
	
	
	public static int getMcount(){
		return mcount.get();
	}
	public static int mcountIncrement(){
		return mcount.incrementAndGet();
	}
	public static int mcountIncrement(int i){
		return mcount.addAndGet(i);
	}
	
	/**
	 * 克隆数据缓存
	 *
	 * @return
	 * @author Chen Tao
	 * @date 2012-7-1 下午07:38:35
	 */
	public static <T extends VtbBaseEntity> HashMap<String,T> synCloneMap(Class<T> clazz){
	    HashMap<String, T> hashMap = null;
	    if (clazz.isInstance(RrOauth.class)) {
	        synchronized (DataCache.rrmap) {
	            hashMap = (HashMap<String, T>) DataCache.rrmap.clone();
	            for (Entry<String,T> entry : hashMap.entrySet()) {
	                try {
	                    hashMap.put(entry.getKey(), (T) entry.getValue().clone());
	                } catch (CloneNotSupportedException e) {
	                    e.printStackTrace();
	                }
	            }
            }
        }
	    if (clazz.isInstance(QqOauth.class)) {
	        synchronized (DataCache.qqmap) {
	            hashMap = (HashMap<String, T>) DataCache.qqmap.clone();
	            for (Entry<String,T> entry : hashMap.entrySet()) {
                    try {
                        hashMap.put(entry.getKey(), (T) entry.getValue().clone());
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                }
            }
	    }
	    if (clazz.isInstance(SsOauth.class)) {
	        synchronized (DataCache.ssmap) {
	            hashMap = (HashMap<String, T>) DataCache.ssmap.clone();
                for (Entry<String,T> entry : hashMap.entrySet()) {
                    try {
                        hashMap.put(entry.getKey(), (T) entry.getValue().clone());
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                }
            }
	    }
		return hashMap;
	}

	/**
	 * 同步放入数据
	 *
	 * @param rrOauth
	 * @author Chen Tao
	 * @date 2012-7-2 下午5:23:37
	 */
	public static <T extends VtbBaseEntity> void synPutMap(T t){
	    if (t.getClass().isInstance(RrOauth.class)) {
            synchronized (DataCache.rrmap) {
                DataCache.rrmap.put(t.getUni(), (RrOauth)t);
            }
        }
        if (t.getClass().isInstance(QqOauth.class)) {
            synchronized (DataCache.qqmap) {
                DataCache.qqmap.put(t.getUni(), (QqOauth)t);
            }
        }
        if (t.getClass().isInstance(SsOauth.class)) {
            synchronized (DataCache.ssmap) {
                DataCache.ssmap.put(t.getUni(), (SsOauth)t);
            }
        }
	}
	
	
	/**
	 * 同步删除数据
	 *
	 * @param t
	 * @author Chen Tao
	 * @date 2012-7-2 下午5:28:20
	 */
	public static <T extends VtbBaseEntity> void synRemMap(T t){
	    if (t.getClass().isInstance(RrOauth.class)) {
            synchronized (DataCache.rrmap) {
                DataCache.rrmap.remove(t.getUni());
            }
        }
        if (t.getClass().isInstance(QqOauth.class)) {
            synchronized (DataCache.qqmap) {
                DataCache.qqmap.remove(t.getUni());
            }
        }
        if (t.getClass().isInstance(SsOauth.class)) {
            synchronized (DataCache.ssmap) {
                DataCache.ssmap.remove(t.getUni());
            }
        }
	}
 }
